home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / htmlsample / htmlsample.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  24.0 KB  |  777 lines

  1. /*
  2.     file HTMLSample.c
  3.     
  4.     Description:
  5.     This file contains the main application program for the HTMLSample.
  6.     Routines in this file are responsible for handling events directed
  7.     at the application.
  8.     
  9.     HTMLSample is an application illustrating how to use the new
  10.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  11.     is Apple's light-weight HTML rendering engine capable of
  12.     displaying HTML files.
  13.  
  14.     by John Montbriand, 1999.
  15.  
  16.     Copyright: © 1999 by Apple Computer, Inc.
  17.     all rights reserved.
  18.     
  19.     Disclaimer:
  20.     You may incorporate this sample code into your applications without
  21.     restriction, though the sample code has been provided "AS IS" and the
  22.     responsibility for its operation is 100% yours.  However, what you are
  23.     not permitted to do is to redistribute the source as "DSC Sample Code"
  24.     after having made changes. If you're going to re-distribute the source,
  25.     we require that you make it clear in the source that the code was
  26.     descended from Apple Sample Code, but that you've made changes.
  27.     
  28.     Change History (most recent first):
  29.     10/16/99 created by John Montbriand
  30. */
  31.  
  32. #include "HTMLSample.h"
  33.  
  34. #include <Menus.h>
  35. #include <Windows.h>
  36. #include <Dialogs.h>
  37. #include <Events.h>
  38. #include <Fonts.h>
  39. #include <SegLoad.h>
  40. #include <Resources.h>
  41. #include <Balloons.h>
  42. #include <Devices.h>
  43. #include <AppleEvents.h>
  44. #include <StdIO.h>
  45. #include <StdArg.h>
  46. #include <string.h>
  47. #include <ToolUtils.h>
  48. #include <Appearance.h>
  49. #include <Navigation.h>
  50. #include <StandardFile.h>
  51.  
  52.  
  53. #include <HTMLRendering.h>
  54.  
  55. #include "RenderingWindow.h"
  56. #include "SampleUtils.h"
  57. #include "AboutBox.h"
  58.  
  59.     /* true while the app is running */
  60. Boolean gRunning = true;
  61.  
  62. #ifndef __MWERKS__
  63. QDGlobals    qd; /* QuickDraw globals */
  64. #endif
  65.  
  66.  
  67.  
  68. /* OpenOneFile is called for each file the application is asked to open
  69.     either by way of Apple event or from the file menu.  spec points to
  70.     a file specification record referring to the file to open.  The file will
  71.     be opened in a new window. */
  72. static OSErr OpenOneFile(FSSpec *spec) {
  73.     Handle urlHandle, errorPageLink;
  74.     WindowPtr rWindow;
  75.     Str255 errStr;
  76.     OSErr err;
  77.         /* initial state */
  78.     urlHandle = NULL;
  79.     rWindow = NULL;
  80.         /* allocate locals */
  81.     urlHandle = NewHandle(0);
  82.     if (urlHandle == NULL) { err = memFullErr; goto bail; }
  83.     errorPageLink = GetResource(kCStyleStringResourceType, kErrorPageURLString);
  84.     if (errorPageLink == NULL)  { err = resNotFound; goto bail; }
  85.         /* convert the fsspec to a url */
  86.     err = HRUtilGetURLFromFSSpec(spec, urlHandle);
  87.     if (err != noErr) goto bail;
  88.         /* open the window */
  89.     err = RWOpen(&rWindow);
  90.     if (err != noErr) goto bail;
  91.         /* attempt to open the url */
  92.     MoveHHi(urlHandle);
  93.     HLock(urlHandle);
  94.     err = RWGotoURL(rWindow, *urlHandle, true);
  95.     HUnlock(urlHandle);
  96.         /* if that fails, try to show the error page */
  97.     if (err != noErr) {
  98.         MoveHHi(errorPageLink);
  99.         HLock(errorPageLink);
  100.         err = RWGotoAppRelLink(rWindow, *errorPageLink, true);
  101.         HUnlock(errorPageLink);
  102.         if (err != noErr) goto bail;
  103.     }
  104.         /* clean up and leave */
  105.     DisposeHandle(urlHandle);
  106.     return noErr;
  107. bail:
  108.         /* we display an alert here if there's an error. returning
  109.         an error from this routine will abort any open routine that
  110.         is going on--even if we're in the middle of a list of files. */
  111.     NumToString(err, errStr);
  112.     ParamAlert(kOpenFileErrorAlert, errStr, spec->name);
  113.     if (rWindow != NULL) RWCloseWindow(rWindow);
  114.     if (urlHandle != NULL) DisposeHandle(urlHandle);
  115.     return err;
  116. }
  117.  
  118. /* IdentifyPackage identifies a Mac OS 9 package and returns a reference
  119.     to it's main file inside of mainPackageFile.  In Mac OS 9, packages are
  120.     the special folders that have their bundle bits set and contain an alias
  121.     at their topmost level referring to a file somewhere in the package. */
  122. static Boolean IdentifyPackage(FSSpec *target, FSSpec *mainPackageFile) {
  123.     CInfoPBRec cat;
  124.     OSErr err;
  125.     long pDir;
  126.     Str255 name;
  127.     FSSpec aliasFile;
  128.     Boolean targetIsFolder, wasAliased;
  129.         /* check the target's flags */
  130.     cat.dirInfo.ioNamePtr = target->name;
  131.     cat.dirInfo.ioVRefNum = target->vRefNum;
  132.     cat.dirInfo.ioFDirIndex = 0;
  133.     cat.dirInfo.ioDrDirID = target->parID;
  134.     err = PBGetCatInfoSync(&cat);
  135.     if (err != noErr) return false;
  136.         /* if it's a folder and the bundle bit is set....*/
  137.     if (((cat.dirInfo.ioFlAttrib & 16) != 0) && ((cat.dirInfo.ioDrUsrWds.frFlags & kHasBundle) != 0)) {
  138.             /* search for a top level alias file.  Here, we enumerate all of the
  139.             objects in the directory until we find a file with the alias flag set. */
  140.         pDir = cat.dirInfo.ioDrDirID;
  141.         cat.dirInfo.ioNamePtr = name;
  142.         cat.dirInfo.ioVRefNum = target->vRefNum;
  143.         cat.dirInfo.ioFDirIndex = 1;
  144.         cat.dirInfo.ioDrDirID = pDir;
  145.         while (PBGetCatInfoSync(&cat) == noErr) {
  146.                 /* if the thing we're looking at is not a directory and it's alias flag is set,
  147.                 try to resolve it as an alias file. */
  148.             if (((cat.dirInfo.ioFlAttrib & 16) == 0) && ((cat.dirInfo.ioDrUsrWds.frFlags & kIsAlias) != 0)) {
  149.                 err = FSMakeFSSpec(target->vRefNum, pDir, name, &aliasFile);
  150.                 if (err != noErr) return false;
  151.                 err = ResolveAliasFile(&aliasFile, false, &targetIsFolder, &wasAliased);
  152.                 if (err != noErr) return false;
  153.                 if (mainPackageFile != NULL)
  154.                     *mainPackageFile = aliasFile;
  155.                 return true;
  156.             }
  157.                 /* move on to the next file in the directory. */
  158.             cat.dirInfo.ioFDirIndex++;
  159.             cat.dirInfo.ioDrDirID = pDir;
  160.         }
  161.     }
  162.         /* we found nothing matching our criteria, so we
  163.         fail. */
  164.     return false;
  165. }
  166.  
  167.  
  168. /* OpenTheDocuments is called to open a list of documents provided by
  169.     either an open documents Apple event or one of the Navigation services
  170.     dialogs.  */
  171. static OSErr OpenTheDocuments(AEDescList *theDocuments) {
  172.     OSErr err;
  173.     long i, n;
  174.     FSSpec fileSpec, packageSpec;
  175.     AEKeyword keyWd;
  176.     DescType typeCd;
  177.     Size actSz;
  178.     
  179.         /* open them */
  180.     err = AECountItems(theDocuments, &n);
  181.     if (err != noErr) goto bail;
  182.         /* and then open each one */
  183.     for (i = 1 ; i <= n; i++) {
  184.             /* get the i'th FSSpec record.  NOTE: implicity, we are calling
  185.             a coercion handler because this list actually contains alias records. 
  186.             In particular, the coercion handler converts them from alias records
  187.             into FSSpec records. */
  188.         err = AEGetNthPtr(theDocuments, i, typeFSS, &keyWd, &typeCd,
  189.             (Ptr) &fileSpec, sizeof(fileSpec), (actSz = sizeof(fileSpec), &actSz));
  190.         if (err != noErr) goto bail;
  191.             /* if it's a package, we'll open it's main file, otherwise
  192.             we'll open the file itself */
  193.         if (IdentifyPackage(&fileSpec, &packageSpec))
  194.             err = OpenOneFile(&packageSpec);
  195.         else err = OpenOneFile(&fileSpec);
  196.         if (err != noErr) goto bail;
  197.     }
  198.     return noErr;
  199. bail:
  200.     return err;
  201. }
  202.  
  203.  
  204. /* MyNavFilterProc This is the filter function we provide for the Navigation services
  205.     dialogs.  We only allow files of type TEXT. */
  206. static pascal Boolean MyNavFilterProc( AEDesc* theItem, void* info, NavCallBackUserData callBackUD, NavFilterModes filterMode) {
  207.     NavFileOrFolderInfo* theInfo;
  208.     if ( theItem->descriptorType == typeFSS ) {
  209.         theInfo = (NavFileOrFolderInfo*) info;
  210.         if ( theInfo->isFolder ) return true;
  211.         if ( theInfo->fileAndFolder.fileInfo.finderInfo.fdType != 'TEXT' )
  212.             return false;
  213.     }
  214.     return true;
  215. }
  216.  
  217.  
  218. /* NavEventCallBack is the event handling call back we provide for Navigation
  219.     services.  It's presence is important so our windows will be updated appropriately
  220.     when the navigation window is resized or moved. */
  221. static pascal void NavEventCallBack( NavEventCallbackMessage callBackSelector,
  222.             NavCBRecPtr callBackParms, NavCallBackUserData callBackUD) {
  223.     if (callBackSelector == kNavCBEvent && callBackParms->eventData.eventDataParms.event->what == updateEvt) {
  224.  
  225.         HandleEvent(callBackParms->eventData.eventDataParms.event);
  226.  
  227.     }
  228. }
  229.  
  230.  
  231. /* MyFileFilterProc is used by the older standard file calls.  We fall back to
  232.     standard file when navigation services is not present or unavailable.  In this
  233.     routine, we filter out all invisible files. */
  234. static pascal Boolean MyFileFilterProc(CInfoPBPtr pb) {
  235.         /* don't display invisible files */
  236.     return ((pb->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) != 0);
  237. }
  238.  
  239.  
  240. /* SelectAndOpenFile is the inner workings of the Open... command
  241.     when it is chosen from the file menu.  Here, we use the navigation
  242.     services dialogs when they are available, but if they're not, then we
  243.     use the standard file ones. */
  244. static OSStatus SelectAndOpenFile(void) {
  245.     NavDialogOptions dialogOptions;
  246.     NavReplyRecord theReply;
  247.     NavEventUPP eventf;
  248.     NavObjectFilterUPP filterf;
  249.     FileFilterUPP stdFilterf;
  250.     Boolean hasreply;
  251.     OSStatus err;
  252.         /* set up locals */
  253.     eventf = NULL;
  254.     filterf = NULL;
  255.     stdFilterf = NULL;
  256.     hasreply = false;
  257.     memset(&theReply, 0, sizeof(theReply));
  258.         /* if Navigation services is available, then we
  259.         use those calls. */
  260.     if (NavServicesAvailable()) {
  261.             /* allocate data */
  262.         filterf = NewNavObjectFilterUPP(MyNavFilterProc);
  263.         if (filterf == NULL) { err = memFullErr; goto bail; }
  264.         eventf = NewNavEventProc(NavEventCallBack);
  265.         if (eventf == NULL) { err = memFullErr; goto bail; }
  266.             /* set up dialog options */
  267.         err = NavGetDefaultDialogOptions(&dialogOptions);
  268.         if (err != noErr) goto bail;
  269.             /* NOTE: by setting the kNavAllowMultipleFiles flag, we make it possible
  270.             for the user to select more than one file.  And, setting the kNavSupportPackages
  271.             flag allows us to open package documents. */
  272.         dialogOptions.dialogOptionFlags = (kNavDontAutoTranslate | kNavAllowMultipleFiles | kNavSupportPackages);
  273.         GetIndString(dialogOptions.message, kMainStringList, kNavMessageString);
  274.             /* pick one or more files */
  275.         err = NavChooseFile(NULL, &theReply, &dialogOptions, eventf,  NULL,  filterf,  NULL, NULL);
  276.         if (err != noErr) goto bail;
  277.         if (!theReply.validRecord) { err = userCanceledErr; goto bail; }
  278.         hasreply = true;
  279.             /* if we have a valid reply, then call our
  280.             open documents routine. */
  281.         err = OpenTheDocuments(&theReply.selection);
  282.         if (err != noErr) goto bail;
  283.             /* clean up the structures we allocated */
  284.         NavDisposeReply(&theReply);
  285.         DisposeNavEventUPP(eventf);
  286.         DisposeNavObjectFilterUPP(filterf);
  287.     } else {
  288.         StandardFileReply reply;
  289.         SFTypeList typeList = { 'TEXT', 'TEXT', 'TEXT', 'TEXT' };
  290.             /* set up our locals */
  291.         stdFilterf = NewFileFilterUPP(MyFileFilterProc);
  292.         if (stdFilterf == NULL) { err = memFullErr; goto bail; }
  293.             /* ask for a file.  NOTE: with standard file
  294.             we can only get one file at a time. */
  295.         StandardGetFile(stdFilterf, 1, typeList, &reply);
  296.         if ( ! reply.sfGood) { err = userCanceledErr; goto bail; }
  297.             /* if a file was chosen, open it. */
  298.         err = OpenOneFile(&reply.sfFile);
  299.         if (err != noErr) goto bail;
  300.             /* clean up the structures we allocated */
  301.         DisposeFileFilterUPP(stdFilterf);
  302.     }
  303.     return noErr;
  304. bail:
  305.     if (hasreply) NavDisposeReply(&theReply);
  306.     if (eventf != NULL) DisposeNavEventUPP(eventf);
  307.     if (filterf != NULL) DisposeNavObjectFilterUPP(filterf);
  308.     if (stdFilterf != NULL) DisposeFileFilterUPP(stdFilterf);
  309.     return err;
  310. }
  311.  
  312.  
  313.  
  314.  
  315. /* ResetMenus is called immediately before all calls to
  316.     MenuSelect or MenuKey.  In this routine, we re-build
  317.     or enable the menus as appropriate depending on the
  318.     current environment */
  319. static void ResetMenus(void) {
  320.     WindowPtr target;
  321.     target = FrontWindow();
  322.         /* if the front most window is a rendering
  323.         window, then we let the window handle the
  324.         menu. */
  325.     if (IsARenderingWindow(target))
  326.         RWResetGotoMenu(target);
  327.     else {
  328.         MenuHandle goMenu;
  329.             /* otherwise, we clear the menu
  330.             and disable all of its commands. */
  331.         goMenu = GetMenuHandle(mGo);
  332.         DisableItem(goMenu, iBack);
  333.         DisableItem(goMenu, iForward);
  334.         DisableItem(goMenu, iHome);
  335.         while (CountMItems(goMenu) >= iGoSep)
  336.             DeleteMenuItem(goMenu, iGoSep);
  337.     }
  338. }
  339.  
  340.  
  341. /* DoMenuCommand is called in response to MenuKey
  342.     or MenuSelect.  Here, we dispatch the menu command
  343.     to its appropriate handler, or if it's a small action
  344.     we do it here. */
  345. static void DoMenuCommand(long rawMenuSelectResult) {
  346.     short menu, item;
  347.         /* decode the MenuSelect result */
  348.     menu = (rawMenuSelectResult >> 16);
  349.     if (menu == 0) return;
  350.     item = (rawMenuSelectResult & 0x0000FFFF);
  351.         /* dispatch on result */
  352.     switch (menu) {
  353.             /* apple menu commands */
  354.         case mApple:
  355.             if (item == iAbout) {
  356.                 WindowPtr aboutBox;
  357.                 OSStatus err;
  358.                     /* open the about box */
  359.                 err = OpenAboutBox(&aboutBox);
  360.                 if (err != noErr) {
  361.                     Str255 errStr;
  362.                     NumToString(err, errStr);
  363.                     ParamAlert(kNoAboutBoxErrorAlert, errStr, NULL);
  364.                 }
  365.             } else if (item >= iFirstAppleItem) {
  366.                     /* if the item selected is below the separator
  367.                     line, then we open it as a desk accessory. */
  368.                 Str255 deskAccName;
  369.                 GetMenuItemText(GetMenuHandle(mApple), item, deskAccName);
  370.                 OpenDeskAcc(deskAccName);
  371.             }
  372.             break;
  373.             
  374.                 /* file menu commands */
  375.         case mFile:
  376.             if (item == iOpen) {
  377.                 SelectAndOpenFile();
  378.             } else if (item == iQuit) {
  379.                 if (CloseRenderingWindows() == noErr)
  380.                     gRunning = false;
  381.             }
  382.             break;
  383.             
  384.                 /* selections in the Go menu are handled by
  385.                 the frontmost rendering window. */
  386.         case mGo:
  387.             {    WindowPtr target;
  388.                 target = FrontWindow();
  389.                 if (IsARenderingWindow(target))
  390.                     RWHandleGotoMenu(target, item);
  391.             }
  392.             break;
  393.  
  394.     }
  395.         /* unhilite the menu bar */
  396.     HiliteMenu(0);
  397. }
  398.  
  399.  
  400. /* QuitAppleEventHandler is our quit Apple event handler.  this routine
  401.     is called when a quit Apple event is sent to our application.  Here,
  402.     we set the gRunning flag to false. NOTE:  it is not appropriate to
  403.     call ExitToShell here.  Instead, by setting the flag to false we
  404.     fall through the bottom of our main loop the next time we're called. 
  405.     Here, if we are unable to close all of the rendering windows,
  406.     we return an error.  This will abort the shutdown process,
  407.     if that's why we were called.  */
  408. static pascal OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  409.     if (CloseRenderingWindows() == noErr) {
  410.         gRunning = false;
  411.         return noErr;
  412.     } else return userCanceledErr;
  413. }
  414.  
  415.  
  416. /* OpenAppleEventHandler is called when our application receives
  417.     an 'open application' apple event.  Here, we put up a window
  418.     referring to the default page. */
  419. static pascal OSErr OpenAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  420.     WindowPtr rWin;
  421.     Handle urlHandle;
  422.     OSErr err;
  423.         /* creat a rendering window. */
  424.     err = RWOpen(&rWin);
  425.     if (err != noErr) return err;
  426.         /* get the link to the default page from the resource
  427.         file. */
  428.     urlHandle = GetResource(kCStyleStringResourceType, kDefaultPageURLString);
  429.     if (urlHandle == NULL) return memFullErr;
  430.         /* lock down the resource and point the rendering
  431.         window at it. */
  432.     MoveHHi(urlHandle);
  433.     HLock(urlHandle);
  434.     RWGotoAppRelLink(rWin, *urlHandle, true);
  435.     HUnlock(urlHandle);
  436.         /* done */
  437.     return noErr;
  438. }
  439.  
  440.  
  441. /* ReOpenAppleEventHandler is called whenever the application receives
  442.     a re-open Apple event.  This will happen if the application is already
  443.     running and the user attempts to open it again by either double clicking
  444.     on its icon in the Finder or by selecting its icon and choosing Open in
  445.     the Finder's file menu. Here, if there is no window showing, then we
  446.     open a new one as we would if an open application event was received. */
  447. static pascal OSErr ReOpenAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  448.     
  449.     if (FrontWindow() == NULL)
  450.         return OpenAppleEventHandler(appleEvt, reply, refcon);
  451.     else return noErr;
  452.     
  453.     return noErr;
  454. }
  455.  
  456.  
  457. /* OpenDocumentsEventHandler is called whenever we receive an open documents
  458.     Apple event.  Here, we extract the list of documents from the event
  459.     and pass them along to our OpenTheDocuments routine. */
  460. static pascal OSErr OpenDocumentsEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  461.     OSErr err;
  462.     AEDescList documents;
  463.     
  464.         /* initial state */
  465.     AECreateDesc(typeNull, NULL, 0, &documents);
  466.     
  467.         /* get the open parameter */
  468.     err = AEGetParamDesc(appleEvt, keyDirectObject, typeAEList, &documents);
  469.     if (err != noErr) goto bail;
  470.     
  471.         /* open the documents */
  472.     err = OpenTheDocuments(&documents);
  473.     if (err != noErr) goto bail;
  474.  
  475. bail:
  476.     AEDisposeDesc(&documents);
  477.     return err;
  478. }
  479.  
  480.  
  481.  
  482.  
  483.  
  484. /* HandleMouseDown is called for mouse down events.  Processing of
  485.     mouse down events in the HTML rendering area of windows is 
  486.     handled by the HTMLRenderinLib, but clicks in the controls and
  487.     other parts of the windows are handled here. */
  488. static void HandleMouseDown(EventRecord *ev) {
  489.     WindowPtr theWindow;
  490.     short partcode;
  491.     partcode = FindWindow(ev->where, &theWindow);
  492.     switch (partcode) {
  493.             /* inside the window's content area */
  494.         case inContent:
  495.             if (theWindow != FrontWindow()) {
  496.                     /* if it's not the frontmost window,
  497.                     then make it the frontmost window. */
  498.                 SelectWindow(theWindow);
  499.             } else {
  500.                     /* otherwise, if it's a rendering window,
  501.                     pass the click along to the window. */
  502.                 Point where;
  503.                 SetPort(theWindow);
  504.                 SetOrigin(0, 0);
  505.                 where = ev->where;
  506.                 GlobalToLocal(&where);
  507.                 if (IsARenderingWindow(theWindow))
  508.                     RWHandleMouseDown(theWindow, where);
  509.             }
  510.             break;
  511.             
  512.             /* menu bar clicks */
  513.         case inMenuBar:
  514.             ResetMenus();
  515.             DoMenuCommand(MenuSelect(ev->where));
  516.             break;
  517.             
  518.             /* track clicks in the close box */
  519.         case inGoAway:
  520.             if (TrackGoAway(theWindow, ev->where)) {
  521.                 if (IsARenderingWindow(theWindow))
  522.                     RWCloseWindow(theWindow);
  523.                 else if (IsAboutBox(theWindow))
  524.                     AboutBoxCloseWindow(theWindow);
  525.             }
  526.             break;
  527.             
  528.             /* allow window drags */
  529.         case inDrag:
  530.             {    Rect boundsRect = {0,0, 32000, 32000};
  531.                 DragWindow(theWindow, ev->where, &boundsRect);
  532.             }
  533.             break;
  534.             
  535.             /* allow window drags */
  536.         case inGrow:
  537.             {    Rect sizerect;
  538.                 long grow_result;
  539.                 SetRect(&sizerect, 300, 150, 32767, 32767);
  540.                 grow_result = GrowWindow(theWindow, ev->where, &sizerect);
  541.                 if (grow_result != 0) {
  542.                     SizeWindow(theWindow, LoWord(grow_result), HiWord(grow_result), true);
  543.                     SetPort(theWindow);
  544.                     InvalRect(&theWindow->portRect);
  545.                     if (IsARenderingWindow(theWindow))
  546.                         RWRecalculateSize(theWindow);
  547.  
  548.                 }
  549.             }
  550.             break;
  551.             
  552.             /* zoom box clicks.  NOTE:  since the rendering window
  553.             always sets the standard rectangle to the 'best size' for
  554.             displaying the current HTML window, the inZoomOut partcode
  555.             will zoom the window to that size rather than the entire screen.*/
  556.         case inZoomIn:
  557.         case inZoomOut:
  558.             SetPort(theWindow);
  559.             EraseRect(&theWindow->portRect);
  560.             ZoomWindow(theWindow, partcode, true);
  561.             SetPort(theWindow);
  562.             if (IsARenderingWindow(theWindow))
  563.                 RWRecalculateSize(theWindow);
  564.             break;
  565.             
  566.             /* desktop clicks, etc... */
  567.         case inSysWindow:
  568.             SystemClick(ev, theWindow);
  569.             break;
  570.     }
  571. }
  572.  
  573.  
  574. /* HandleEvent is the main event handling routine for the
  575.     application.  ev points to an event record returned by
  576.     WaitNextEvent. */
  577. void HandleEvent(EventRecord *ev) {
  578.     WindowPtr target;
  579.         /* process menu key events */
  580.     if (((ev->what == keyDown) || (ev->what == autoKey)) && ((ev->modifiers & cmdKey) != 0)) {
  581.         ResetMenus();
  582.         DoMenuCommand(MenuKey((char) (ev->message & charCodeMask)));
  583.         ev->what = nullEvent;
  584.     }
  585.     
  586.         /* process HR events.  NOTE: this call handles most of the events
  587.         for the active HTML rendering object.  But, be careful it may set
  588.         the clip region and origin of that window to an unknown state. */
  589.     if (HRIsHREvent(ev))
  590.         ev->what = nullEvent;
  591.     
  592.         /* process other event types */
  593.     switch (ev->what) {
  594.             /* the application may be switching in to the forground
  595.             or into the background.  Either way, we need to activate
  596.             the frontmost window accordingly. */
  597.         case osEvt:
  598.             target = FrontWindow();
  599.             if (IsARenderingWindow(target))
  600.                 RWActivate(target, ((ev->message & resumeFlag) != 0));
  601.             else if (IsAboutBox(target))
  602.                 AboutBoxActivate(target, ((ev->message & resumeFlag) != 0));
  603.             break;
  604.             
  605.             /* for activate events we call the window's activate event
  606.             handler. */
  607.         case activateEvt:
  608.             target = (WindowPtr) ev->message;
  609.             if (IsARenderingWindow(target))
  610.                 RWActivate(target, ((ev->modifiers&1) != 0));
  611.             else if (IsAboutBox(target))
  612.                 AboutBoxActivate(target, ((ev->modifiers&1) != 0));
  613.             break;
  614.             
  615.             /* for update events we call the window's update event
  616.             handler. if the window is of an unknown type, we ignore the
  617.             event. */
  618.         case updateEvt:
  619.             target = (WindowPtr) ev->message;
  620.             if (IsARenderingWindow(target))
  621.                 RWUpdate(target);
  622.             else if (IsAboutBox(target))
  623.                 AboutBoxUpdate(target);
  624.             else {
  625.                 BeginUpdate(target);
  626.                 EndUpdate(target);
  627.             }
  628.             break;    
  629.             
  630.             /* for mouse events we call the the HandleMouseDown routine
  631.             defined above. */
  632.         case mouseDown:
  633.             HandleMouseDown(ev);
  634.             break;
  635.         
  636.             /* for key down events we call the window's key down event
  637.             handler. */
  638.         case keyDown:
  639.         case autoKey:
  640.             target = FrontWindow();
  641.             if (IsARenderingWindow(target))
  642.                 RWKeyDown(target, (char) (ev->message & charCodeMask));
  643.             break;
  644.         
  645.             /* Apple events. */
  646.         case kHighLevelEvent:
  647.             AEProcessAppleEvent(ev);
  648.             break;
  649.     }
  650. }
  651.  
  652.  
  653. /* FDPIdleProcedure is the idle procedure called by AEInteractWithUser while we are waiting
  654.     for the application to be pulled into the forground.  It simply passes the event along
  655.     to HandleNextEvent */
  656. static pascal Boolean MyIdleInteractProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) {
  657.     HandleEvent(theEvent);
  658.     return ( ! gRunning ); /* quit waiting if we're not running */
  659. }
  660.  
  661.  
  662. /* ParamAlert is a general alert handling routine.  If Apple events exist, then it
  663.     calls AEInteractWithUser to ensure the application is in the forground, and then
  664.     it displays an alert after passing the s1 and s2 parameters to ParamText. */
  665. short ParamAlert(short alertID, StringPtr s1, StringPtr s2) {
  666.     AEIdleUPP aeIdleProc;
  667.     OSStatus err;
  668.     aeIdleProc = NewAEIdleUPP(MyIdleInteractProc);
  669.     if (aeIdleProc == NULL) { err = memFullErr; goto bail; }
  670.     err = AEInteractWithUser(kNoTimeOut, NULL, aeIdleProc);
  671.     if (err != noErr) goto bail;
  672.     ParamText(s1, s2, NULL, NULL);
  673.     err = Alert(alertID, NULL);
  674.     DisposeAEIdleUPP(aeIdleProc);
  675.     return err;
  676. bail:
  677.     if (aeIdleProc != NULL) DisposeAEIdleUPP(aeIdleProc);
  678.     return err;
  679. }
  680.  
  681.  
  682. /* MyGrowZone is called by the Memory Manager whenever it
  683.     cannot fulfil a memory request.  Here, we try to get back
  684.     enough memory for the request by asking the HTML rendering
  685.     library to free up some cache space. */
  686. static pascal long MyGrowZone(Size cbNeeded) {
  687.     return HRFreeMemory(cbNeeded);
  688. }
  689.  
  690.  
  691.  
  692.     /* the main program */
  693.  
  694. int main(void) {
  695.     OSStatus err;
  696.     Boolean isAppearanceClient;
  697.     Str255 errStr;
  698.     
  699.     isAppearanceClient = false;
  700.     
  701.         /* set up the macintosh managers */
  702.     SetApplLimit(GetApplLimit());
  703.     MaxApplZone();
  704.     InitGraf(&qd.thePort);
  705.     InitFonts();
  706.     InitWindows();
  707.     TEInit();
  708.     InitMenus();
  709.     InitDialogs(0);
  710.     FlushEvents(everyEvent, 0);
  711.     InitCursor();
  712.     
  713.         /* register ourselves with the appearance manager. */
  714.     err = RegisterAppearanceClient();
  715.     if (err != noErr) goto bail;
  716.     isAppearanceClient = true;
  717.         
  718.         /* install our event handlers */
  719.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(OpenAppleEventHandler), 0, false);
  720.     if (err != noErr) goto bail;
  721.     err = AEInstallEventHandler(kCoreEventClass, 'rapp', NewAEEventHandlerProc(ReOpenAppleEventHandler), 0, false);
  722.     if (err != noErr) goto bail;
  723.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(OpenDocumentsEventHandler), 0, false);
  724.     if (err != noErr) goto bail;
  725.     err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(QuitAppleEventHandler), 0, false);
  726.     if (err != noErr) goto bail;
  727.  
  728.         /* set up the menu bar */
  729.     SetMenuBar(GetNewMBar(kMenuBarID));
  730.     DrawMenuBar();
  731.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
  732.  
  733.         /* set up the rendering library */
  734.     if ( ! HRHTMLRenderingLibAvailable()) {
  735.         ParamAlert(kNoRenderingLibErrorAlert, NULL, NULL);
  736.         err = userCanceledErr;
  737.         goto bail;
  738.     }
  739.     
  740.         /* install our memory manger grow zone
  741.         routine. */
  742.     SetGrowZone(NewGrowZoneUPP(MyGrowZone));
  743.     
  744.         /* initialize the rendering windows library */
  745.     err = InitRenderingWindows();
  746.     if (err != noErr) goto bail;
  747.     
  748.         /* run the app */
  749.     while (gRunning) {
  750.         EventRecord ev;
  751.         
  752.             /* get the next event */
  753.         if ( ! WaitNextEvent(everyEvent, &ev,  GetCaretTime(), NULL))
  754.             ev.what = nullEvent;
  755.             
  756.             /* call our handler to deal with it. */
  757.         HandleEvent(&ev);
  758.  
  759.     }
  760.     
  761.         /* close all of our windows. */
  762.     CloseRenderingWindows();
  763.     EnsureAboutBoxIsClosed();
  764.     
  765.         /* unregister ourselves with the appearance manager. */
  766.     UnregisterAppearanceClient();
  767.     ExitToShell();
  768.     return 0;
  769. bail:
  770.     NumToString(err, errStr);
  771.     if (err != userCanceledErr)
  772.         ParamAlert(kOpenApplicationErrorAlert, errStr, NULL);
  773.     if (isAppearanceClient)
  774.         UnregisterAppearanceClient();
  775.     ExitToShell();
  776.     return 0;
  777. }